package pl.edu.agh.winmon.analise;

import java.util.Date;

import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import pl.edu.agh.winmon.hibernate.CpuLoad;
import pl.edu.agh.winmon.hibernate.Session;
import pl.edu.agh.winmon.hibernate.Turn;

public class AverageCpuSeriesProvider implements SessionSeriesProvider {

	public TimeSeriesCollection provideSeries(Session session, long start,
			long end) {
		TimeSeries timeSeries = new TimeSeries("Average CPU usage");
		for (Turn turn : session.getTurns()) {
			if (turn != null) {
				if (turn.getStartTime() >= start && turn.getStartTime() < end) {
					Double value = 0.0;
					int size = 0;
					for (CpuLoad cpuLoad : turn.getCpusLoads()) {
						if (cpuLoad != null) {
							value += cpuLoad.getCpuLoad();
							size++;
						}
					}

					value = value / size * 100;
					timeSeries.addOrUpdate(
							new Second(new Date(turn.getStartTime())), value);
				}
			}
		}
		TimeSeriesCollection collection = new TimeSeriesCollection(timeSeries);
		return collection;
	}

}
